<?php
/**
 * Fang_Service_Article
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-03-09 23:48:27
 * 修改记录: 
 * 
 * $Id: Article.php 18 2011-04-17 12:50:07Z coosuu $
 */

class Fang_Service_Article {

    /**
     * add 
     * 
     * @param  mixed $data 
     * @return void
     */
    public function add($data) {

        try {
            $res = Fang_DAO_Article::add($data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
        return $res;
    }

    /**
     * update 
     * 
     * @param  mixed $aid 
     * @param  mixed $data 
     * @return void
     */
    public function update($aid, $data) {

        try {
            $res = Fang_DAO_Article::update($aid, $data);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
        return $res;
    }

    /**
     * remove
     * 删除记录 
     * 
     * @param  integer $aid 
     * @return void
     */
    public function remove($aid) {

        try {
            $res = Fang_DAO_Article::remove($aid);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
        return $res;
    }

    /**
     * listByCid
     * 根据分类列表 
     * 
     * @param  integer $cid 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return array
     */
    public function listByCid($cid, &$pageOptions = array(), $fields = array()) {

        if ($pageOptions) {
            if (!$pageOptions['totalItems']) {
                $pageOptions['totalItems'] = Fang_DAO_Article::countByCid($cid);
            }
            $pageOptions = Pager::convert($pageOptions);
        }

        try {
            return Fang_DAO_Article::listByCid($cid, $pageOptions, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * listAll 
     * 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public function listAll(&$pageOptions = array(), $fields = array()) {

        if ($pageOptions) { 
            if (!$pageOptions['totalItems']) {
                $pageOptions['totalItems'] = Fang_DAO_Article::countAll();
            }
            $pageOptions = Pager::convert($pageOptions);
        }

        try {
            return Fang_DAO_Article::listAll($pageOptions, $fields);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * listByIds 
     * 
     * @param  mixed $ids 
     * @return void
     */
    public function listByIds($ids) {

        try {
            return Fang_DAO_Article::listByIds($ids);
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }

    /**
     * getInfo 
     * 
     * @param  mixed $aid 
     * @param  array $fields 
     * @return void
     */
    public function getInfo($aid, $fields = array()) {

        try {
            $data = Fang_DAO_Article::getInfo($aid, $fields);
            if ($data['a_content']) {
                $data['a_content'] = json_decode($data['a_content'], true);
            }
            return $data;
        } catch (Exception $e) {
            Logger::error($e);
            throw new Service_Exception($e);
        }
    }
}
